#!/bin/bash

FILE_PATH=$1
# 获取文件路径的所属目录
DIR_NAME=$(dirname "$FILE_PATH")

# 获取文件的文件名
FILE_NAME=$(basename "$FILE_PATH")
FILE_NAME_WITHOUT_EXT="${FILE_NAME%.*}"  # 关键操作：删除最后一个点及其后的所有字符

# 获取脚本所在目录
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" && pwd )
# 获取根目录
ROOT_DIR=$(dirname "$SCRIPT_DIR")
ROOT_DIR=$(dirname "$ROOT_DIR")

BUILD_SCRIPT_DIR=$ROOT_DIR/script/example

echo ROOT_DIR=$ROOT_DIR
echo SCRIPT_DIR=$SCRIPT_DIR

mkdir -p $DIR_NAME/build

OUT_LL_PATH=${DIR_NAME}/build/${FILE_NAME_WITHOUT_EXT}.ll
echo "To ll: ${OUT_LL_PATH}"
$BUILD_SCRIPT_DIR/clang_to_ll.sh ${FILE_PATH}
echo ""

echo "To asm: ${DIR_NAME}/build/$FILE_NAME_WITHOUT_EXT.s"
$BUILD_SCRIPT_DIR/clang_to_asm.sh ${FILE_PATH}
echo ""

echo "To bc: ${DIR_NAME}/build/$FILE_NAME_WITHOUT_EXT.bc"
$BUILD_SCRIPT_DIR/clang_to_bc.sh ${FILE_PATH}
echo ""

echo "To exe: ${DIR_NAME}/build/$FILE_NAME_WITHOUT_EXT"
$BUILD_SCRIPT_DIR/clang_to_exe.sh ${FILE_PATH}
echo ""

# echo "Test Pass helloworld: ${OUT_LL_PATH}"
# opt --passes=helloworld -S ${OUT_LL_PATH} -disable-output
# $BUILD_SCRIPT_DIR/clang_to_bc_use_ollvm.sh ${FILE_PATH}
# echo ""

echo "To exe use ollvm: ${DIR_NAME}/build/${FILE_NAME_WITHOUT_EXT}_ollvm"
$BUILD_SCRIPT_DIR/clang_to_exe_use_ollvm.sh ${FILE_PATH}
echo ""